<!--
 * @Description: 票务机票订单数据统计
 * @Version: 2.0
 * @Autor: 许国辉
 * @Date: 2022-11-27 14:09:03
 * @LastEditors: 许国辉
 * @LastEditTime: 2024-12-17 11:51:51
-->
<template>
  <div>
    <PageWrapper>
      <BasicTable @register="registerTable">
        <template #toolbar>
          <a-button type="primary" @click="customHeader"> 导出表格 </a-button>
        </template>
        <template #bodyCell="{ column, record }">
          <!-- 航空公司 -->
          <template v-if="column.key === 'carrierName'">
            {{ record.carrierName + '(' + record.carrierCode + ')' }}
          </template>
          <!-- 改签费 -->
          <template v-if="column.key === 'changeUpFeechangeFee'">
            {{ record.changeUpFee + record.changeFee }}
          </template>
          <!-- 舱位 -->
          <template v-if="column.key === 'cabinCode'">
            {{ record.cabinDesc + '(' + record.cabinCode + ')' }}
          </template>
          <!-- 机票价salePrice -->
          <template v-if="column.key === 'salePrice'">
            <span v-if="record.bookingType === '改期单'">0</span>
            <span v-else>{{ record.salePrice }}</span>
          </template>
          <!-- 税（taxSum） -->
          <template v-if="column.key === 'taxSum'">
            <span v-if="record.bookingType === '改期单'">0</span>
            <span v-else>{{ record.taxSum }}</span>
          </template>
          <!-- 机票合计金额（planePriceSum）  -->
          <template v-if="column.key === 'planePriceSum'">
            <span v-if="record.bookingType === '改期单'">0</span>
            <span v-else>{{ record.planePriceSum }}</span>
          </template>
          <!-- 保险（insurance） -->
          <template v-if="column.key === 'insurance'">
            <span v-if="record.bookingType === '退票单'">-{{ record.insurance }} </span>
            <span v-else>{{ record.insurance }}</span>
          </template>
          <!-- 折扣率（agio） -->
          <template v-if="column.key === 'agio'">
            <span v-if="record.bookingType === '改期单'">0</span>
            <span v-else>{{ record.agio }}</span>
          </template>
          <!-- 服务费（ticketServicePrice）  -->
          <!-- <template v-if="column.key === 'ticketServicePrice'">
            <span v-if="record.bookingType === '改期单'">0</span>
            <span v-else>{{ record.ticketServicePrice }}</span>
          </template> -->
          <!-- 特别说明isOutLimit -->
          <template v-if="column.key === 'isOutLimit'">
            <div
              v-if="
                record.isOutLimit === '1' &&
                record.bookingType !== '改期单' &&
                record.bookingType !== '退票单'
              "
              style="background-color: #5182ed"
              >{{ record.outLimitReason }}</div
            >
            <div v-else>{{
              record.isOutLimit === 1 ? '超标' + ' ' + record.outLimitReason : '未超标'
            }}</div>
          </template>
        </template>
      </BasicTable>
    </PageWrapper>
  </div>
</template>
<script lang="ts">
  // import { Tabs } from 'ant-design-vue';
  import { defineComponent, onMounted, ref, toRaw } from 'vue';
  // import { PageWrapper } from '/@/components/Page';
  import { BasicTable, useTable } from '/@/components/Table';
  import { columns, searchFormSchema } from './orderStatisticsAirPersonalData';
  import { jsonToSheetXlsx } from '/@/components/Excel';
  import { getOrderStatisticsAir } from '/@/api/orderStaiistics/air/orderStaiisticsAPI';
  import { OrderStatisticsAirModel } from '/@/api/orderStaiistics/air/OrderStatisticsAirModel';
  import dayjs from 'dayjs';
  export default defineComponent({
    components: {
      BasicTable,
      // [Tabs.name]: Tabs,
      // [Tabs.TabPane.name]: Tabs.TabPane,
    },
    setup() {
      let api = ref(getOrderStatisticsAir);
      const [registerTable, { getDataSource }] = useTable({
        api,
        pagination: { pageSize: 10 },
        searchInfo: { bookingWay: 2 },
        columns: columns,
        showIndexColumn: true,
        formConfig: {
          labelWidth: 100,
          actionColOptions: { span: 24 },
          schemas: searchFormSchema,
          fieldMapToTime: [
            ['outTicketDate', ['outTicketDateStart', 'outTicketDateEnd'], 'YYYY-MM-DD'],
            ['deptDate', ['deptDateStart', 'deptDateEnd'], 'YYYY-MM-DD'],
          ],
        },
        striped: false,
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        // showTableSetting: true,
        tableSetting: { fullScreen: true },
      });

      // async function tabChange(activeKey) {
      //   if (activeKey === '1') {
      //     api.value = getOrderStatisticsAirBybookingWay1;
      //     await getForm().submit;
      //     reload();
      //   } else if (activeKey === '2') {
      //     api.value = getOrderStatisticsAirBybookingWay2;
      //     await getForm().submit;
      //     reload();
      //   }
      // }
      function customHeader() {
        let header = {
          index: '序号',
          linkman: '终端类型',
          outTicketDate: '出票日期',
          bookerName: '预定人姓名',
          bookerTel: '预定人手机',
          exNum: '审批单号',
          exDepartureDate: '审批出发日期',
          exReturnDate: '审批返回日期',
          empNo: '员工编号',
          pnr: 'PNR',
          cabinAllPrice: '全价',
          tickerNo: '票号',
          empName: '员工姓名',
          empTel: '联系手机',
          voyage: '行程',
          flightNum: '航班号',
          carrierName: '航空公司',
          deptDate: '出发日期',
          deptTime: '出发时间',
          arriveDate: '抵达日期',
          arriveTime: '抵达时间',
          cabinCode: '舱位',
          salePrice: '机票价',
          taxSum: '税',
          planePriceSum: '机票合计金额',
          changeUpFeechangeFee: '改签费',
          refundBeAmount: '应退款(含税)',
          insurance: '保险',
          a3: '用车',
          a4: '其它',
          agio: '折扣率',
          ticketServicePrice: '服务费',
          priceBy: '深白云扣款',
          refundPrice: '退票费(财务)',
          ticketPrice: '应收',
          ticketPriceSum: '总金额包含服务费',
          a5: '订票方式',
          isSettlement: '结算',
          isAccount: '白云对帐',
          payDate: '支付时间',
          bookingType: '订票类型',
          belongType: '机票类型',
          remark: '备注',
          empIdNum: '身份证号',
          a12: '消费记录id',
          // a13: '订单号',
          a14: '乘机人卡号',
          a15: '出发城市',
          a16: '到达地',
          a17: '员工部门',
          a70: '订单号',
        };

        let tableData: OrderStatisticsAirModel[] = toRaw(getDataSource());

        let excalDateArr: any = [];
        for (let i = 0; i < tableData.length; i++) {
          let excalDate = {
            index: 0,
            linkman: '终端类型',
            outTicketDate: '出票日期',
            bookerName: '预定人姓名',
            bookerTel: '预定人手机',
            exNum: '审批单号',
            exDepartureDate: '审批出发日期',
            exReturnDate: '审批返回日期',
            empNo: '员工编号',
            pnr: 'PNR',
            cabinAllPrice: 0,
            tickerNo: '票号',
            empName: '员工姓名',
            empTel: '联系手机',
            voyage: '行程',
            flightNum: '航班号',
            carrierName: '航空公司',
            deptDate: '出发日期',
            deptTime: '出发时间',
            arriveDate: '抵达日期',
            arriveTime: '抵达时间',
            cabinCode: '舱位',
            salePrice: 0,
            taxSum: 0,
            planePriceSum: 0,
            changeUpFeechangeFee: 0,
            refundBeAmount: 0,
            insurance: 0,
            a3: 0,
            a4: 0,
            agio: 0,
            ticketServicePrice: 0,
            priceBy: 0,
            refundPrice: 0,
            ticketPrice: 0,
            ticketPriceSum: 0,
            a5: '无审批单',
            isSettlement: '结算',
            isAccount: '白云对帐',
            payDate: '支付时间',
            bookingType: '订票类型',
            belongType: '机票类型',
            remark: '备注',
            empIdNum: '身份证号',
            a12: '消费记录id',
            // a13: '订单号',
            a14: '乘机人卡号',
            a15: '出发城市',
            a16: '到达地',
            a17: '员工部门',
            a70: '订单号',
          };
          let record = tableData[i];
          excalDate.index = i + 1;
          if (tableData[i].linkman === '票务') {
            excalDate.linkman = 'PC';
          } else {
            excalDate.linkman = 'APP';
          }
          excalDate.outTicketDate = tableData[i].outTicketDate
            ? dayjs(record.outTicketDate).format('YYYY-M-D HH:mm:ss')
            : '-';
          excalDate.bookerName = tableData[i].bookerName;
          excalDate.bookerTel = tableData[i].bookerTel;
          excalDate.exNum = tableData[i].exNum ? tableData[i].exNum : '-';
          excalDate.exDepartureDate = '-';
          excalDate.exReturnDate = '-';
          excalDate.empNo = tableData[i].empNo;
          excalDate.pnr = tableData[i].pnr;
          excalDate.cabinAllPrice = tableData[i].cabinAllPrice;
          excalDate.tickerNo = tableData[i].tickerNo;
          excalDate.empName = tableData[i].empName;
          excalDate.empTel = tableData[i].empTel;
          excalDate.voyage = tableData[i].voyage;
          excalDate.flightNum = tableData[i].flightNum;
          excalDate.carrierName = tableData[i].carrierName + '(' + tableData[i].carrierCode + ')';
          excalDate.deptDate = tableData[i].deptDate
            ? dayjs(record.deptDate).format('YYYY-MM-DD')
            : '-';
          excalDate.deptTime = tableData[i].deptTime;
          excalDate.arriveDate = tableData[i].arriveDate
            ? dayjs(record.arriveDate).format('YYYY-MM-DD')
            : '-';
          excalDate.arriveTime = tableData[i].arriveTime;
          excalDate.cabinCode = tableData[i].cabinDesc + '(' + tableData[i].cabinCode + ')';
          excalDate.ticketServicePrice = tableData[i].ticketServicePrice;
          if (tableData[i].bookingType === '改期单') {
            excalDate.salePrice = 0;
            excalDate.taxSum = 0;
            excalDate.planePriceSum = 0;
            excalDate.agio = 0;
            // excalDate.ticketServicePrice = 0;
          } else {
            excalDate.salePrice = tableData[i].salePrice;
            excalDate.taxSum = tableData[i].taxSum;
            excalDate.planePriceSum = tableData[i].planePriceSum;
            excalDate.agio = tableData[i].agio;
            excalDate.ticketServicePrice = tableData[i].ticketServicePrice;
          }
          excalDate.changeUpFeechangeFee = tableData[i].changeFee + tableData[i].changeUpFee;
          excalDate.refundBeAmount = tableData[i].refundBeAmount;
          if (tableData[i].bookingType === '退票单') {
            excalDate.insurance = -tableData[i].insurance;
          } else {
            excalDate.insurance = tableData[i].insurance;
          }
          excalDate.a3 = 0;
          excalDate.a4 = 0;
          excalDate.refundPrice = tableData[i].refundPrice;

          // 深白云扣款 应收
          if (tableData[i].bookingType === '改期单') {
            excalDate.priceBy = tableData[i].changeUpFee + tableData[i].changeFee;
            excalDate.ticketPrice = tableData[i].changeFee + tableData[i].changeUpFee;
            excalDate.ticketPriceSum =
              tableData[i].changeFee + tableData[i].changeUpFee + tableData[i].ticketServicePrice;
          } else if (tableData[i].bookingType === '退票单') {
            excalDate.priceBy =
              tableData[i].refundPrice +
              tableData[i].ticketPrice -
              tableData[i].insurance +
              tableData[i].changeUpFee +
              tableData[i].changeFee;
            excalDate.ticketPrice =
              tableData[i].refundPrice +
              tableData[i].ticketPrice -
              tableData[i].insurance +
              tableData[i].changeUpFee +
              tableData[i].changeFee;
            excalDate.ticketPriceSum =
              tableData[i].refundPrice +
              tableData[i].ticketPriceSum +
              tableData[i].changeUpFee +
              tableData[i].changeFee +
              tableData[i].insurance;
          } else {
            excalDate.priceBy = tableData[i].ticketPrice + tableData[i].insurance;
            excalDate.ticketPrice = tableData[i].ticketPrice + tableData[i].insurance;
            excalDate.ticketPriceSum = tableData[i].ticketPriceSum + tableData[i].insurance;
          }
          excalDate.a5 = '无审批单';
          if (tableData[i].isSettlement === 0) {
            excalDate.isSettlement = '未结算';
          } else {
            excalDate.isSettlement = '已结算';
          }
          if (tableData[i].isAccount === 0) {
            excalDate.isAccount = '未对帐';
          } else {
            excalDate.isAccount = '已对帐';
          }
          excalDate.payDate = tableData[i].payDate
            ? dayjs(tableData[i].payDate).format('YYYY-MM-DD')
            : '-';
          excalDate.bookingType = tableData[i].bookingType;
          excalDate.belongType = tableData[i].belongType;
          excalDate.remark = tableData[i].remark ? tableData[i].remark : '-';
          excalDate.empIdNum = 'ID:' + tableData[i].empIdNum;
          excalDate.a12 = tableData[i].id.toString();
          // excalDate.a13 = tableData[i].orderNum;
          excalDate.a14 = tableData[i].empIdNum;
          excalDate.a15 = tableData[i].orgName;
          excalDate.a16 = tableData[i].dstName;
          excalDate.a17 = tableData[i].departmentId ? tableData[i].departmentId : '-';
          excalDate.a70 = tableData[i].orderNum;
          excalDateArr.push(excalDate);
        }

        jsonToSheetXlsx({
          data: excalDateArr,
          header,
          filename: '机票订单数据统计(无审批单)' + dayjs(new Date()).format('YYYY-MM-DD') + '.xlsx',
        });
      }

      onMounted(() => {});

      return {
        registerTable,
        customHeader,
      };
    },
  });
</script>
